/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: EncryptionUtil.java
 * Author: ckj
 * Date: 2024/7/21 下午10:17
 * Description:
 * 加密相关的工具类
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;


/**
 * @author ckj
 */
public class EncryptionUtil {
    public static final String AES_KEY_STRING = "wsWcvZhevZ2QzkL+f3x8dw==";
    public static final byte[] AES_KEY = Base64.getDecoder().decode(AES_KEY_STRING);

    public static String encode(String data) throws Exception {
        byte[] byteArray = data.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return new String(byteArray, StandardCharsets.UTF_8);
    }

    public static String decrypt(String data) throws Exception {
        byte[] byteArray = data.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return new String(byteArray, StandardCharsets.UTF_8);
    }
}